package Model;

import java.util.ArrayList;

/**
 *
 * @author Harsh
 */
public class Board {

    private int numOfSquares;
    public ArrayList<Square> squares;
    private ArrayList<Upper> uppers = new ArrayList(5);
    private ArrayList<Downer> downers = new ArrayList(5);
    private int width;
    private int height;
    //end of instance variables

    public Board(int m, ArrayList<Integer> n, ArrayList<Integer> o) {
        numOfSquares = m;
        System.out.println("num of squares " + numOfSquares);
        squares = new ArrayList(numOfSquares);
        for (int x = 0; x < numOfSquares; x++) {
            int temp = x + 1;
          squares.add(new Square(temp));
        }  
        
        /*for (int i = 0; i < numOfSquares; i++) {
            System.out.println(i + " in board " + squares.get(i).getNumber());
        }*/
        
        setUpper(n);
        setDowner(o);
    }
    //End of default constructor

    public Board(int n, int w, int h) {
        numOfSquares = n;
        for (int x = 0; x < n; x++) {
            squares.add(new Square(x));
        }
        width = w;
        height = h;
    }//End custom board constructor  

    public int getHeight() {
        return height;
    }//End method getHeight

    public int getWidth() {
        return width;
    }//End method getWidth

    public int getNumOfSquares() {
        return numOfSquares;
    }//End method getNumofSquares

    public Square getWinningSquare() {
        int n = squares.size();
        return squares.get(n - 1);
    }//End method getWinningSquare

    public void addSquares(Square s) {
        squares.add(s);
    }//End method addSquare
  
    public void setUpper(ArrayList<Integer> ups) {

        for(int i = 0; i < ups.size(); i = i+2) {
            uppers.add(new Upper(new Square(ups.get(i)), new Square(ups.get(i + 1))));
        }

        for(int i = 0; i < uppers.size(); i++) {
        System.out.println("uppers " + uppers.get(i).getStart() + " end  " + uppers.get(i).getEnd());
        }
    }//End method setUpper

    public void setDowner(ArrayList<Integer> downs) {

         for(int i = 0; i < downs.size(); i = i+2) {
            downers.add(new Downer(new Square(downs.get(i)), new Square(downs.get(i + 1))));
         }

         for(int i = 0; i < downers.size(); i++) {
         System.out.println("downers " + downers.get(i).getStart() + " end  " + downers.get(i).getEnd());
         }
    }//End method setDowner

    public int findEnd(int n) {
        int end = 0;
        for (int i = 0; i < uppers.size(); i++) {
            if (uppers.get(i).getStart() == n) {
                end = uppers.get(i).getEnd();
            }
        }
        for (int i = 0; i < downers.size(); i++) {
            if (downers.get(i).getStart() == n) {
                end = downers.get(i).getEnd();
            }
        }
        return end;
    }//To find the end (destination square) of an Upper or a Downer

    public boolean checkDowns(int currentPos) {

        for (int i = 0; i < downers.size(); i++) {
            if (downers.get(i).getStart() == currentPos) {
                System.out.println("Oh no! landed on a downer on square: " + downers.get(i).getStart());
                return true;
            }
        }
        return false;
    } // To check if a square contains the start of a Downer

    public boolean checkUps(int currentPos) {
        for (int i = 0; i < uppers.size(); i++) {
            if (uppers.get(i).getStart() == currentPos) {
                System.out.println("Yes! landed on an upper on square: " + uppers.get(i).getStart());
                return true;
            }
        }
        return false;
    } // To check if a square contains the start of an Upper
}//End of class Board

